<?php
use yii\helpers\Html;
use kartik\grid\GridView;

/* @var $this yii\web\View */
/* @var $searchModel common\models\goods\GoodsSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '商品';
$this->params['breadcrumbs'][] = $this->title;

$saleType = [''=>'不过滤',0=>'下架',1=>'上架'];

?>
<div class="goods-index">

    <div id="goodsSearch">
    <?php echo $this->render('_search', ['model' => $searchModel]); ?>
    </div>
    
    <p>
        <?= Html::a('新建', ['create'], ['class' => 'btn btn-success']) ?> 
        <?= Html::button('高级搜索', ['class' => 'btn btn-primary','onclick'=>'listSearch();']) ?>
    </p>

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],

            'id',
            'name',
            //'category_id',
            //'goods_type',
            //'brand_id',
            // 'img',
            [
                'attribute' => 'is_on_sale',
                'filterType'=>GridView::FILTER_SELECT2,
                'filter'=>$saleType, 
                'content' => function($data, $row) {
                    return $data->is_on_sale==1?'<div class="green">上架</div>':'<div class="red">下架</div>';
                },
            ],
            // 'is_del',
            // 'add_time:datetime',
            // 'last_update',
             'remark',
            // 'supplier_id',
            // 'pack_unit',
            // 'class_id',
            // 'price_retail',
            // 'price_mix',
            // 'price_pack',
            // 'is_stock_disable',
            // 'is_auto_sale_on',
            // 'is_auto_sale_off',
             'stock',

            [
                'class' => 'yii\grid\ActionColumn',
                'buttons'=>[
                    'view' ,
                    'update' => function ($url, $model, $key) {
                        return Html::a('<span class="glyphicon glyphicon-pencil"></span>', ['update','id'=>$model->id],['target'=>'_blank']);
                    },
                    'delete',
                ],
            ],
        ],
    ]); ?>

</div>



<?php
function isListSearch($attr){
    foreach ($attr as $key => $val) {
        if($key!='is_del' && $key!='supplier_id' && $val!=''){
            return true;
        }
    }
    return false;
}

?>

<style>
    <?php if(isListSearch($searchModel->attributes)==FALSE):?>#goodsSearch{display:none;}<?php endif;?>
</style>

<script>
    function listSearch(){
        $("#goodsSearch").toggle("fast");
    }
</script>